package com.viewpagerindicator.sample.fragents;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.viewpagerindicator.sample.GoodsDetailActivity;
import com.viewpagerindicator.sample.R;
import com.viewpagerindicator.sample.adapters.GoodsListAdapter;
import com.viewpagerindicator.sample.beans.Goods;
import com.viewpagerindicator.sample.util.URLs;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

public final class FirstFragment extends Fragment {
    private static final String KEY_CONTENT = "TestFragment:Content";

    public static FirstFragment newInstance(String content) {
        FirstFragment fragment = new FirstFragment();
        return fragment;
    }
    private View view;//缓存Fragment view

    private String mContent = "???";
    ArrayList<Goods> goodsArrayList=new ArrayList<>();
    private ListView listView;
    private GoodsListAdapter adapter;
    private View listviewBg; //列表试图
    private View progressingBg;//等待试图;
    private View errormsgBg;//错误试图;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
            mContent = savedInstanceState.getString(KEY_CONTENT);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(view==null){
            view= inflater.inflate(R.layout.first_layout,container,false);

            listView= (ListView) view.findViewById(R.id.first_layout_listview);
            listviewBg=  view.findViewById(R.id.list_bg);
            progressingBg=  view.findViewById(R.id.progress_bg);
            errormsgBg=  view.findViewById(R.id.errormsg_bg);
            adapter=new GoodsListAdapter(getActivity(),R.layout.goods_listview_item,goodsArrayList);
            listView.setAdapter(adapter);
            errormsgBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listviewBg.setVisibility(View.GONE);
                    progressingBg.setVisibility(View.VISIBLE);
                    errormsgBg.setVisibility(View.GONE);
                    initData();
                }
            });
            initData();
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_CONTENT, mContent);
    }
    /**
     * 获取商品
     * */
    public void initData(){
        if(goodsArrayList!=null&&!goodsArrayList.isEmpty()){
            listviewBg.setVisibility(View.VISIBLE);
            progressingBg.setVisibility(View.GONE);
            errormsgBg.setVisibility(View.GONE);
            return ;
        }
        AsyncHttpClient client = new AsyncHttpClient();
        client.get(URLs.getRecommend, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                listviewBg.setVisibility(View.VISIBLE);
                progressingBg.setVisibility(View.GONE);
                errormsgBg.setVisibility(View.GONE);
                //解析数据
                try{
                    List<Goods> temp=Goods.parseList(responseBody);
                    if(temp!=null){
                        goodsArrayList.clear();
                        goodsArrayList.addAll(temp);
                        adapter.notifyDataSetChanged();
                    }
                }catch (Exception e){
                    Toast.makeText(getActivity(),getString(R.string.net_error),Toast.LENGTH_SHORT).show();
                    listviewBg.setVisibility(View.GONE);
                    progressingBg.setVisibility(View.GONE);
                    errormsgBg.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Toast.makeText(getActivity(),getString(R.string.net_error),Toast.LENGTH_SHORT).show();
                listviewBg.setVisibility(View.GONE);
                progressingBg.setVisibility(View.GONE);
                errormsgBg.setVisibility(View.VISIBLE);
            }
        });
    }
}
